﻿<!--回到滚动区域的顶部-->
<div id="ph-gotop">

</div>

<script>
    var goTop = null;
    var goTopContainer = null;
    var ticking = false;

    function onGoTop(ev) {
        $(goTopContainer).animate({ scrollTop: 0 }, 'ease');
    };

    function onScrollRaw(ev) {
        if (goTopContainer.scrollTop > 500 && goTop.css("display") == "none") {
            goTop.fadeIn(500, function () {ticking = false; onScroll(); });
        }
        else if (goTopContainer.scrollTop <= 500 && goTop.css("display") == "block") {
            goTop.fadeOut(200, function () {ticking = false; onScroll(); });
        }
        else {
            ticking = false;
        }
    };

    function onScroll(ev) {
        if (!ticking) {
            requestAnimationFrame(onScrollRaw);
            ticking = true;
        }
    };

    $(function () {
        goTop = $("#ph-gotop");
        goTop.on("click", onGoTop);
        goTopContainer = goTop.parent().parent();  //滚动容器
        if (goTopContainer.is("html")) {
            goTopContainer = goTopContainer[0];
            window.addEventListener("scroll", onScroll);
        }
        else {
            goTopContainer = goTopContainer[0];
            goTopContainer.addEventListener("scroll", onScroll);
        }
    });
</script>